import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_simple_watch/news/bean/we_chat_news.dart';
import 'package:flutter_simple_watch/news/viewmodel/we_chat_news_view_model.dart';
import 'package:provider/provider.dart';

import '../base/base_result.dart';

///@author aidenquan
/// Created by junlintianxia on 1/14/21.

class WeChatNewsPage extends StatefulWidget {
  const WeChatNewsPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _WeChatNewsPageState();
  }
}

class _WeChatNewsPageState extends State<WeChatNewsPage> {
  final WeChatNewsViewModel _viewModel = WeChatNewsViewModel();

  @override
  void initState() {
    super.initState();
    _viewModel.getWeChatNews("", 20, true);
  }

  Future<void> _pullToRefresh() async {
    _viewModel.getWeChatNews("", 20, true);
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<WeChatNewsViewModel>.value(
        value: _viewModel,
        child: Scaffold(
          backgroundColor: Color.fromARGB(255, 255, 0, 0),
          appBar: AppBar(
            backgroundColor: Color.fromARGB(255, 0, 255, 0),
            title: InkWell(
              onTap: () {
                // Fluttertoast.showToast(
                //     msg: "微信新闻",
                //     toastLength: Toast.LENGTH_SHORT,
                //     gravity: ToastGravity.CENTER,
                //     timeInSecForIosWeb: 1,
                //     backgroundColor: Colors.red,
                //     textColor: Colors.white,
                //     fontSize: 16.0
                // );
                // ScaffoldMessenger.of(context).showSnackBar(
                //   SnackBar(
                //       content: Text(
                //           '微信新闻')),
                // );
                BotToast.showText(text: '微信新闻');
              },
              child: const Text('微信新闻'),),
            centerTitle: true,
          ),
          body: Container(
            child: Consumer(
              builder: (BuildContext context, WeChatNewsViewModel viewModel,
                  Widget? child) {
                if (viewModel.baseResult == null) {
                  return const Center(
                    child: CircularProgressIndicator(),
                  );
                } else if (viewModel.baseResult?.code ==
                    BaseResult.NETWORK_ERROR) {
                  // 网络请求失败
                  return const Center(
                    child: Text("网络请求失败"),
                  );
                } else {
                  print("微信新闻${viewModel.baseResult?.data}");
                  var wechatResult =
                      WeChatResponse.formJson(viewModel.baseResult?.data)
                          .result;
                  Widget listView = ListView.separated(
                      itemBuilder: (context, index) {
                        return ListTile(
                            title: Text('${wechatResult?.list[index].title}'));
                      },
                      separatorBuilder: (context, index) => const Divider(height: .0),
                      itemCount: wechatResult?.list.length ?? 0);
                  return RefreshIndicator(
                      onRefresh: _pullToRefresh,
                      child: listView);
                }
              },
            ),
          ),
        ));
  }
}
